package com.adtech.farmProducts.activities.activity;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.adtech.common.Common;
import com.adtech.common.http.AsyncHttpPostFormData;
import com.adtech.common.http.AsyncHttpResponse;
import com.adtech.common.http.AsyncOkHttpClient;
import com.adtech.common.http.AsyncResponseHandler;
import com.adtech.common.model.RspModel;
import com.adtech.common.utils.AccountManagers;
import com.adtech.common.utils.TimeUtil;
import com.adtech.farmProducts.App;
import com.adtech.farmProducts.R;
import com.adtech.farmProducts.base.BaseActivity;
import com.adtech.farmProducts.base.BaseNfcActivity;
import com.adtech.farmProducts.model.FUserInfo;
import com.adtech.farmProducts.model.RetailPrice;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.lang.reflect.Type;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Time:2020/7/2
 * Created by xiaocz
 * Describe 价格详情
 */
public class RetailPriceInfoActivity extends BaseNfcActivity {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_type)
    TextView tv_type;
    @BindView(R.id.tv_name)
    TextView tv_name;
    @BindView(R.id.tv_address)
    TextView tv_address;
    @BindView(R.id.tv_gs_name)
    TextView tv_gs_name;
    @BindView(R.id.tv_price)
    TextView tv_price;

    private RetailPrice retailPrice;


    @Override
    protected int getLayoutSourceId() {
        return R.layout.activity_retail_price_info;
    }

    public static void show(Context context, RetailPrice retailPrice) {
        Intent intent = new Intent(context, RetailPriceInfoActivity.class);
        intent.putExtra("retailPrice", retailPrice);
        context.startActivity(intent);
    }

    @Override
    protected boolean initArgs(Bundle bundle) {
        if (bundle != null) {
            retailPrice = (RetailPrice) bundle.getSerializable("retailPrice");
        }
        return super.initArgs(bundle);
    }

    @Override
    protected void initData() {
        tvTitle.setText("产品信息");
        if (retailPrice != null) {
            tv_type.setText(retailPrice.getProductTypeName());
            tv_name.setText(retailPrice.getProductName());
            tv_address.setText(retailPrice.getAddress());
            tv_gs_name.setText(retailPrice.getCompanyName());
            tv_price.setText(retailPrice.getUnitPriceStr());
        }
        super.initData();
    }

    @OnClick({R.id.btn_update})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_update:
                onDialogNext();
                break;
        }
    }

    private void onDialogNext() {
        final Dialog alert = new Dialog(mContext);
        alert.requestWindowFeature(Window.FEATURE_NO_TITLE);
        alert.setContentView(R.layout.dialog_update_price);
        TextView textView=alert.findViewById(R.id.tv_title);
        textView.setText(String.format("现在价格：%s",retailPrice.getUnitPriceStr()));
        final EditText editText=alert.findViewById(R.id.ed_price);
        alert.findViewById(R.id.btn_exit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                alert.dismiss();
            }
        });
        alert.findViewById(R.id.btn_submit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String value=String.valueOf(editText.getText()).trim();
                if("".equals(value)){
                    App.getInstance().showMessage("请输入新价格！");
                    return;
                }
                updatePrice(retailPrice.getProductId(),value);
                alert.dismiss();
            }
        });
        alert.show();
        Window window = alert.getWindow();
        if (window != null) {
            window.setBackgroundDrawableResource(android.R.color.transparent);
            WindowManager.LayoutParams lp = alert.getWindow().getAttributes();
            DisplayMetrics dm = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(dm);
            lp.width = (int) (dm.widthPixels * 0.88); //设置宽度
            alert.getWindow().setAttributes(lp);
        }
    }

    /**
     * 修改单价
     */
    private void updatePrice(String productId, String unitPrice) {
        loadingDialogShow(false);
        AsyncOkHttpClient okHttpClient = new AsyncOkHttpClient();
        AsyncHttpPostFormData asyncHttpPostFormData = new AsyncHttpPostFormData();
        asyncHttpPostFormData.addFormData("companyId",new Gson().fromJson(AccountManagers.getAccount(), FUserInfo.class).getInfo().getCompanyId());
        asyncHttpPostFormData.addFormData("productId", productId);
        asyncHttpPostFormData.addFormData("unitPrice", unitPrice);
        okHttpClient.posts(Common.Constant.API_IP + "productUnitPrice/resetPrice", asyncHttpPostFormData, new AsyncResponseHandler() {
            @Override
            public void onFailure(IOException e) {
                onDismiss();
                App.getInstance().showMessage("修改单价失败！");
                e.printStackTrace();
            }

            @Override
            public void onSuccess(AsyncHttpResponse response) {
                onDismiss();
                Type type = new TypeToken<RspModel<String>>() {
                }.getType();
                RspModel<String> rspModel = getAnalysis(response, type, "商品零售价格");
                if (rspModel == null) {
                    App.getInstance().showMessage("修改单价失败！");
                    return;
                }
                if (rspModel.getCode() == 1) {
                    App.getInstance().showMessage("修改单价成功！");
                    setResult(200);
                    finish();
                } else {
                    App.getInstance().showMessage("修改单价失败！");
                }
            }
        });
    }
}
